/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 - 2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.gwt.form.client.validator.impl;

import com.genia.toolbox.web.gwt.basics.client.i18n.GwtI18nMessage;
import com.genia.toolbox.web.gwt.basics.client.support.GwtObjectUtils;

/**
 * implementation of <code>Validator</code> that checks if a value matche with
 * the given reqular expression.
 */
public class RegularExpressionValidator
    extends AbstractSimpleValueSynchronousValidator
{
  /**
   * default error message.
   */
  public static final String DEFAULT_PARSE_ERROR_KEY = "com.genia.toolbox.web.gwt.form.client.validator.impl.RegularExpressionValidator.parse_error";

  /**
   * {@link GwtI18nMessage} error message.
   */
  private GwtI18nMessage errorMessage;

  /**
   * the regular expression.
   */
  private String regularExpression;



  /**
   * constructor.
   */
  public RegularExpressionValidator()
  {
    this(null);
  }



  /**
   * constructor.
   * 
   * @param regularExpression
   *          the regular expression
   */
  public RegularExpressionValidator(String regularExpression)
  {
    this(regularExpression, null);
  }



  /**
   * constructor.
   * 
   * @param regularExpression
   *          the regular expression
   * @param errorMessage
   *          {@link GwtI18nMessage} error message
   */
  public RegularExpressionValidator(String regularExpression, GwtI18nMessage errorMessage)
  {
    setRegularExpression(regularExpression);
    setErrorMessage(errorMessage);
  }



  /**
   * getter for the errorMessage property.
   * 
   * @return the errorMessage
   */
  public GwtI18nMessage getErrorMessage()
  {
    return errorMessage;
  }



  /**
   * setter for the errorMessage property.
   * 
   * @param errorMessage
   *          the errorMessage to set
   */
  public void setErrorMessage(GwtI18nMessage errorMessage)
  {
    this.errorMessage = errorMessage;
  }



  /**
   * getter for the regularExpression property.
   * 
   * @return the regularExpression
   */
  public String getRegularExpression()
  {
    return regularExpression;
  }



  /**
   * setter for the regularExpression property.
   * 
   * @param regularExpression
   *          the regularExpression to set
   */
  public void setRegularExpression(String regularExpression)
  {
    this.regularExpression = regularExpression;
  }



  /**
   * validate if value matche to the regular expression.
   * 
   * @param value
   *          the value to match.
   * @return {@link GwtI18nMessage} message.
   */
  @Override
  public GwtI18nMessage validate(String value)
  {
    if (value.matches(getRegularExpression())) {
      return null;
    }
    if (getErrorMessage() != null) {
      return getErrorMessage();
    }
    return new GwtI18nMessage(DEFAULT_PARSE_ERROR_KEY);
  }



  /**
   * Indicates whether some other object is "equal to" this one.
   * 
   * @param obj
   *          the reference object with which to compare.
   * @return <code>true</code> if this object is the same as the obj argument;
   *         <code>false</code> otherwise.
   * @see java.lang.Object#equals(java.lang.Object)
   */
  @Override
  public boolean equals(final Object obj)
  {
    if (obj == this) {
      return true;
    }
    if (obj == null) {
      return false;
    }
    // if (getClass() == obj.getClass()) { Waiting for gwt with java 1.5
    if (obj instanceof RegularExpressionValidator) {
      final RegularExpressionValidator oItem = (RegularExpressionValidator) obj;

      if (!GwtObjectUtils.nullSafeEquals(getRegularExpression(), oItem.getRegularExpression())) {
        return false;
      }

      return GwtObjectUtils.nullSafeEquals(getErrorMessage(), oItem.getErrorMessage());

    }

    return false;
  }



  /**
   * Returns a hash code value for the object. This method is supported for the
   * benefit of hashtables such as those provided by
   * <code>java.util.Hashtable</code>.
   * 
   * @return a hash code value for this object.
   * @see java.lang.Object#hashCode()
   */
  @Override
  public int hashCode()
  {
    return GwtObjectUtils.nullSafeHashCode(new Object[] {
    // getClass(), Waiting for gwt with java 1.5
        getRegularExpression(), getErrorMessage() });
  }

}
